/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.gui;

 
import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.NotificationManager;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.control.WeatherManager;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.EventNotification;
import it.polimi.se2.meteocal.entity.ParticipationNotification;
import it.polimi.se2.meteocal.entity.Users;
import it.polimi.se2.meteocal.entity.Weather;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
 
/**
 *
 * @author Ugo
 */
@ManagedBean
@SessionScoped
public class NotificationCheckBean  implements Serializable {
   
    @EJB
    private UserManager userManager;
     
    @EJB
    private CalendarManager calendarManager;
    
    @EJB
    private NotificationManager notificationManager;
    
    @EJB
    private WeatherManager weatherManager;
    
    private Users userLogged;
    
    
    @PostConstruct
    public void init(){
        userLogged=userManager.getLoggedUser();
    }
    
    public void check() {
       ParticipationNotification partn; 
       partn=notificationManager.findParticipationNotificationByUserNotSeen(userLogged);
       if(partn!=null)
       {
           partn.setSeen(Boolean.TRUE); 
           notificationManager.edit(partn);
           Event event= partn.getParticipation().getEvent();
           Users userown = calendarManager.findUserByCalendar(event.getCalendar().getId());
           if(partn.getType()== Long.valueOf(1)){
                FacesContext context = FacesContext.getCurrentInstance(); 
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", "New invitation from: " + userown.getEmail() + " for: "  + event.getTitle()));
           }
           else if(partn.getType()==Long.valueOf(2))
                {
                FacesContext context = FacesContext.getCurrentInstance(); 
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", "Event: " + event.getTitle() + " updated from: "  + userown.getEmail()));
                }
           else if(partn.getType()==Long.valueOf(3))
           {
               Weather weather = weatherManager.getWeatherByLocationAndDate(event.getLocation(),event.getData());
               FacesContext context = FacesContext.getCurrentInstance(); 
               context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", weather.getDescription() + " for Event: " + event.getTitle() + " in: "  + event.getLocation() + " on: " + event.getData()));
           }
      } 
       
       EventNotification eventn=null;
       List<Event> eventmycal= calendarManager.findEventsInCalendar(calendarManager.findCalendarByUser(userLogged));
       for(Event e:eventmycal){
           eventn=notificationManager.findEventNotificationByEventNotSeen(e);
           if(eventn!=null){
               eventn.setSeen(Boolean.TRUE); 
               notificationManager.edit(eventn);
               //Weather weather = weatherManager.getWeatherByLocationAndDate(eventn.getEvent().getLocation(),eventn.getEvent().getData());
               FacesContext context = FacesContext.getCurrentInstance(); 
               context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", "Recommended postponement due to bad weather for Event: " + eventn.getEvent().getTitle() + " in: "  + eventn.getEvent().getLocation() + " from: " + eventn.getEvent().getData() + " to: " + eventn.getNewdate()));
                break;
           }
             
       }
    }
    
    
}